/*
 * @author Jerry
 * 命名空间: Jerry
 */

var Jerry = {};

/*
 * Interface Class
 * 接口类，用来创建接口
 * 参数1: 接口名称
 * 参数2: 方法名（数组）
 */
Jerry.Interface = function (name, methods) {
	if(arguments.length != 2){
		throw new Error('Interface constructor called with "+arguments.length+"arguments,but expected exactly 2');
	}
 	this.name = name;
 	this.mothods = [];
 	for(var i = 0; i < methods.length; i++){
 		if(typeof methods[i] !== 'string'){
 			throw new Error('Interface constructor expects mothed names to be'+'passes in as a string');
 		}
 		this.mothods.push(methods[i]);
 	}
}
/*
 * Interface static method
 * 参数1: 实现类
 * 参数2: 接口类
 */
Jerry.Interface.ensureImplements = function (object) {
 	if(arguments.length < 2){
 		throw new Error('Interface constructor called with "+arguments.length+"arguments,but expected exactly 2');
 	}
 	for(var i = 1; i < arguments.length; i++){
 		var instanceInterface = arguments[i];
 		if(instanceInterface.constructor !== Jerry.Interface){
 			throw new Error('the arguments constructor not be Interface Class');
 		}
 		for(var j = 0; j < instanceInterface.mothods.length; j++){
 			var mothedName = instanceInterface.mothods[j];
 			if(!object[mothedName] || typeof object[mothedName] !== 'function'){
 				throw new Error('the mothed name ' + mothedName + ' is not found');
 			}
 		}
 	}
}

/*
 * extends method
 * 继承类
 */
Jerry.extend = function (sub, sup) {
	//目的：实现只继承父类的原型对象
	var f = new Function();	//创建中转函数
	f.prototype = sup.prototype;	//实现中转函数的原型对象和父类原型对象的转换
	sub.prototype = new f();	//原型继承
	//还原子类的构造器
	Object.defineProperty(sub.prototype, 'constructor', {
		enumerable: false, //设置能否被枚举
		value: sub
	})
	//自定义属性记录父类原型对象
	sub.superClass = sup.prototype;
	//判断父类原型的构造器
	if(sup.prototype.constructor == Object.prototype.constructor){
		Object.defineProperty(sup.prototype, 'constructor', {
			enumerable: false, //设置能否被枚举
			value: sup
		})
	}
}

/*
 * 实现一个跨浏览器的事件处理程序
 */
Jerry.eventUtil = {
	addHandler: function (element, type, fn) {
		var e = element instanceof jQuery ? element[0] : element;
		if(window.addEventListener){	//FF
			e.addEventListener(type, fn, false);
		} else if(window.attachEvent){	//IE
			e.attachEvent('on'+type, fn);
		} else {
			e['on'+type] = fn;
		}
	},
	removeHandler: function (element, type, fn) {
		var e = element instanceof jQuery ? element[0] : element;
		if(window.removeEventListener){	//FF
			e.removeEventListener(type, fn, false);
		} else if(window.detachEvent){	//IE
			e.detachEvent('on'+type, fn);
		} else {
			e['on'+type] = null;
		}
	}
}





